package com.maclogic.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.maclogic.utils.Stat;

public class DAO_tab_articles {
	
	private SQLiteDatabase _myDataBase;

	public DAO_tab_articles(Context pContext) {
		SQLiteIntit lsqlinit=new SQLiteIntit(pContext, Stat.DB_NAME,null,1);
		
		// ouvertude de la bd
		_myDataBase=lsqlinit.getWritableDatabase();
		
	}
	
	public void close ()
	{
		_myDataBase.close();
	}
	
	public Cursor selectAll(String pCond)
	{
		String lSQL="SELECT * FROM "+Stat.TABLE_NAME;
		
		//Cursor mCursor=_myDataBase.query(Stat.TABLE_NAME, new String[] {Stat.COL_ID,
		//Stat.COL_REF, Stat.COL_CONST, Stat.COL_PRIX, Stat.COL_CARAC},null, null, null, null, null);
		
		if(pCond!=null && !pCond.equals(""))
			lSQL+=" "+pCond;
		
		//System.out.println(lSQL);
		
		return _myDataBase.rawQuery(lSQL, null);
//		if (mCursor != null) {
//			   mCursor.moveToFirst();
//			  }
//		
//		return mCursor;
		
	}
	
	public Cursor selectArt(int pid)
	{
		String lSQL="SELECT * FROM "+Stat.TABLE_NAME+ " WHERE "+Stat.COL_ID+"="+pid;
		
		//System.out.println(lSQL);
		
		return _myDataBase.rawQuery(lSQL, null);
		
	}
	
	public ArrayList<Article> getListFromCursor(Cursor pCursor){

		ArrayList<Article> lList=new ArrayList<Article>();

		if(pCursor.getCount()!=0){

			pCursor.moveToFirst();

			while(!pCursor.isAfterLast()){


				int lID=pCursor.getInt(pCursor.getColumnIndex(Stat.COL_ID));
				String lRef=pCursor.getString(pCursor.getColumnIndex(Stat.COL_REF));
				String lConst=pCursor.getString(pCursor.getColumnIndex(Stat.COL_CONST));
				double lPrix=pCursor.getDouble(pCursor.getColumnIndex(Stat.COL_PRIX));
				String lCarac=pCursor.getString(pCursor.getColumnIndex(Stat.COL_CARAC));


				Article lArticle=new Article(lID, lRef, lConst, lPrix, lCarac);

				lList.add(lArticle);

				pCursor.moveToNext();
			}
		}
		return lList;
	}
	
	

}
